
<template>
  <el-dialog v-model="dialogFormVisible" class="dialog-title" :title="title" width="80%">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px">
      <el-form-item label="模版名称" prop="name">
        <el-input v-model="temp.name" />
      </el-form-item>
      <el-form-item label="模版主题" prop="templateSubject">
        <el-input v-model="temp.templateSubject" />
      </el-form-item>
      <el-form-item label="模版内容" prop="templateContent">
        <el-input id="templateContentTinymce" v-model="temp.templateContent" :height="500" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button type="primary" :loading="loading" @click="title==='新增'?createData():updateData()">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
<script>
import { createTemplate, updateTemplate } from '@/api/email/template'

export default {
  name: 'TemplateForm',
  emits: ['refreshList'],
  data() {
    return {
      temp: {
        id: undefined,
        name: '',
        code: '',
        businessType: '',
        templateSubject: '',
        templateContent: '',
        configId: '',
        appId: ''

      },
      rules: {
        appId: [{ required: true, message: '请选择所属应用' }],
        configId: [{ required: true, message: '请选择签名配置' }],
        businessType: [{ required: true, message: '请选择业务类型' }],
        name: [{ required: true, message: '模版名称必填' }],
        templateSubject: [{ required: true, message: '模版ID必填' }],
        templateContent: [{ required: true, message: '模版内容必填' }]
      },
      loading: false,
      dialogFormVisible: false,
      title: undefined
    }
  },
  methods: {
    /**
     * 刷新列表
     */
    refreshList() {
      this.$emit('refreshList')
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        name: '',
        code: '',
        businessType: '',
        templateSubject: '',
        templateContent: '',
        configId: '',
        appId: ''
      }
    },
    /**
     * 新增
     */
    handleCreate() {
      this.resetTemp()
      this.title = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 提交新增
     */
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          createTemplate(this.temp).then((response) => {
            this.loading = false
            if (response.data.success) {
              this.dialogFormVisible = false
              this.refreshList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    /**
     * 编辑
     * @param row 当前列表选中数据
     */
    handleUpdate(row) {
      this.resetTemp()
      this.temp = Object.assign({}, row) // copy obj
      this.title = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 提交编辑
     */
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const tempData = Object.assign({}, this.temp)
          updateTemplate(tempData).then((response) => {
            this.loading = false
            if (response.data.success) {
              this.dialogFormVisible = false
              this.refreshList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
